﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.IO;
using Microsoft.Win32;
using ShapeDemo.UC.WinProp;

//pgBody初始化后加一个空点，主要为了下一个点的预览效果，鼠标单击的时候只是对这个点赋值，然后再加入一个新的空点。

namespace ShapeDemo
{
    /// <summary>
    /// 主窗体,主要负责各个层的调度调度
    /// </summary>
    public partial class MainForm : Window
    {
        private string filename = System.Environment.CurrentDirectory + "/ShapeXML/";

        private WinPropPoint WinPointProp = new WinPropPoint();
        private WinPropPolyline winPolylineProp = new WinPropPolyline();
        private WinPropPolygon winPolygonProp = new WinPropPolygon();
        private WinPropRectangle winRectProp = new WinPropRectangle();
        private WinPropEllipse winEllipseProp = new WinPropEllipse();

        private ECanvas Toplayer;
        //private TMouse MouseType;

        private PolygonDrawer pgnDrawer;
        private PolylineDrawer plDrawer;
        private PointDrawer ptDrawer;
        private EllipseDrawer elDrawer;
        private RectDrawer rtDrawer;

        private DataIntoXML dtXML;

        
        public MainForm()
        {
            InitializeComponent();

            //MouseType = TMouse.Drawing;

            propertyCanvas.Children.Add(new UC.Empty());
            
            //创建持久化图形的文件夹
            if (!System.IO.Directory.Exists(filename))
            {
                System.IO.Directory.CreateDirectory(filename);
            }

            //实例化各绘制类
            pgnDrawer = new PolygonDrawer(polygonCanvas, winPolygonProp,winCommonProp);
            plDrawer = new PolylineDrawer(polylineCanvas, winPolylineProp, winCommonProp);
            ptDrawer = new PointDrawer(pointCanvas, WinPointProp, winCommonProp);
            elDrawer = new EllipseDrawer(ellipseCanvas, winEllipseProp, winCommonProp);
            rtDrawer = new RectDrawer(rectCanvas, winRectProp, winCommonProp);

            dtXML = new DataIntoXML(ptDrawer, rtDrawer, elDrawer, plDrawer, pgnDrawer);

            rbPoint.Checked+=new RoutedEventHandler(rbPoint_Checked);
            rbDraw.Checked+=new RoutedEventHandler(rbDraw_Checked);

            this.KeyDown += new KeyEventHandler(MainForm_KeyDown);
            this.KeyUp += new KeyEventHandler(MainForm_KeyUp);
        }

        void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftShift)
            {
                rtDrawer.IsShiftDown = false;
                elDrawer.IsShiftDown = false;
                plDrawer.IsShiftDown = false;
                pgnDrawer.IsShiftDown = false;
            }
        }

        void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftShift)
            {
                rtDrawer.IsShiftDown = true;
                elDrawer.IsShiftDown = true;
                plDrawer.IsShiftDown = true;
                pgnDrawer.IsShiftDown = true;
            }
        }

        /// <summary>
        /// 处理哪一个层放到最上面
        /// </summary>
        /// <param name="ec"></param>
        private void ShowDCanvas()
        {
            polylineCanvas.SetValue(Canvas.ZIndexProperty, 0);
            polygonCanvas.SetValue(Canvas.ZIndexProperty, 0);
            pointCanvas.SetValue(Canvas.ZIndexProperty, 0);
            rectCanvas.SetValue(Canvas.ZIndexProperty, 0);
            ellipseCanvas.SetValue(Canvas.ZIndexProperty, 0);

            switch (Toplayer)
            { 
                case ECanvas.Polyline:
                    polylineCanvas.SetValue(Canvas.ZIndexProperty, 6);

                    propertyCanvas.Children.Clear();
                    propertyCanvas.Children.Add(winPolylineProp);

                    break;
                case ECanvas.Polygon:
                    polygonCanvas.SetValue(Canvas.ZIndexProperty, 6);

                    propertyCanvas.Children.Clear();
                    propertyCanvas.Children.Add(winPolygonProp);

                    break;
                case ECanvas.Point:
                    pointCanvas.SetValue(Canvas.ZIndexProperty, 6);

                    propertyCanvas.Children.Clear();
                    propertyCanvas.Children.Add(WinPointProp);

                    break;
                case ECanvas.Rect:
                    rectCanvas.SetValue(Canvas.ZIndexProperty, 6);

                    propertyCanvas.Children.Clear();
                    propertyCanvas.Children.Add(winRectProp);

                    break;
                case ECanvas.Ellipse:
                    ellipseCanvas.SetValue(Canvas.ZIndexProperty, 6);

                    propertyCanvas.Children.Clear();
                    propertyCanvas.Children.Add(winEllipseProp);

                    break;
            }
        }

        #region 处理按钮事件
        private void btnPolyline_Click(object sender, RoutedEventArgs e)
        {
            Toplayer = ECanvas.Polyline;
            ShowDCanvas();

            winCommonProp.tbIndex.Text = "0";
            winCommonProp.tbContent.Text = "Unnamed polyline";

            plDrawer.Draw();

            plDrawer.BindListBox();
        }

        private void btnPolygon_Click(object sender, RoutedEventArgs e)
        {
            Toplayer = ECanvas.Polygon;
            ShowDCanvas();

            winCommonProp.tbIndex.Text = "0";
            winCommonProp.tbContent.Text = "Unnamed polygon";

            pgnDrawer.Draw();

            pgnDrawer.BindListBox();
        }

        private void btnPoint_Click(object sender, RoutedEventArgs e)
        {
            Toplayer = ECanvas.Point;
            ShowDCanvas();

            winCommonProp.tbIndex.Text = "0";
            winCommonProp.tbContent.Text = "Unnamed point";

            ptDrawer.BindListBox();
        }

        private void btnRect_Click(object sender, RoutedEventArgs e)
        {
            Toplayer = ECanvas.Rect;
            ShowDCanvas();

            winCommonProp.tbIndex.Text = "0";
            winCommonProp.tbContent.Text = "Unnamed Rect";

            rtDrawer.BindListBox();
        }

        private void btnEllipse_Click(object sender, RoutedEventArgs e)
        {
            Toplayer = ECanvas.Ellipse;
            ShowDCanvas();

            winCommonProp.tbIndex.Text = "0";
            winCommonProp.tbContent.Text = "Unnamed Ellipse";

            elDrawer.BindListBox();
        }


        /// <summary>
        /// 关闭程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            dtXML.Save(filename);

            MessageBox.Show("Done!");
        }

        /// <summary>
        /// 从保存的文件中加载图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            dtXML.Load(filename);
        }
        #endregion

        /// <summary>
        /// 窗体的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void viewRectTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #region 绘画状态和选择状态的切换
        /// <summary>
        /// 设置选择模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbPoint_Checked(object sender, RoutedEventArgs e)
        {
            pgnDrawer.MouseTye = TMouse.Point;
            elDrawer.MouseTye = TMouse.Point;
            rtDrawer.MouseTye = TMouse.Point;
            plDrawer.MouseTye = TMouse.Point;
        }

        /// <summary>
        /// 设置绘画模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbDraw_Checked(object sender, RoutedEventArgs e)
        {
            pgnDrawer.MouseTye = TMouse.Drawing;
            elDrawer.MouseTye = TMouse.Drawing;
            rtDrawer.MouseTye = TMouse.Drawing;
            plDrawer.MouseTye = TMouse.Drawing;
        }
        #endregion


        /// <summary>
        /// About窗体弹出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            About aboutFrom = new About();
            aboutFrom.ShowDialog();
        }
    }
}